package com.xunji.xunji.db.dao;

import android.content.Context;

import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.dao.Dao;
import com.xunji.xunji.db.DatabaseHelper;
import com.xunji.xunji.module.trace.dao.Trace;
import com.xunji.xunji.module.trace.dao.TraceRecord;

import java.sql.SQLException;

/**
 * Created by Paul on 2017/2/27.
 */

public class TraceRecordDao {

    private Dao<TraceRecord, Integer> dao;

    public TraceRecordDao(Context context) {
        DatabaseHelper dbHelper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
        try {
            dao = dbHelper.getDao(TraceRecord.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 保存一条用户信息
    public void save(TraceRecord bean) {
        try {
            // 如果表中没有该用户则保存，根据主键是否相同来标示是否是同一用户
            dao.createIfNotExists(bean);

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public void update(TraceRecord record){
        try {
            dao.update(record);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(int id){
        try {
            dao.deleteById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
